package com.changgou.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 记录用户的请求的IP地址
 */
@Component
public class IpGateway implements GlobalFilter, Ordered {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String hostName = request.getRemoteAddress().getHostName();
         logger.info("记录用户请求的ip地址==={}",hostName);
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
